package com.hanhai.zrb.backend.web.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.backend.admin.model.BackendUser;
import com.hanhai.zrb.backend.admin.model.Privilege;
import com.hanhai.zrb.backend.admin.service.UserRoleService;
import com.hanhai.zrb.backend.util.BackendLoginUtil;
import com.hanhai.zrb.backend.util.MenuUtil;

@RequestMapping
@Controller
public class IndexController extends BaseController {

	@Resource
	private UserRoleService userRoleService;

	@RequestMapping("index")
	public String index2(Model model) {
		return index(model);
	}

	@RequestMapping("")
	public String index(Model model) {
		/*
		 * CrmLoginUser user= CrmLoginUtil.getCurrentUser(); if(user!=null){
		 * List<Privilege> privilegeList = userRoleService
		 * .findAllByUserId(user.getId()); List<Privilege> privilegeList =
		 * backUserService .findAllByUserId(1L);
		 * 
		 * model.addAttribute("privilegeList", privilegeList); }
		 */

		return "index";
	}

	// 首页默认的欢迎内容
	@RequestMapping("welcome")
	public String welcome(Model model) {
		return "welcome";
	}

	@RequestMapping("/getMenu")
	public void getMenu(HttpServletResponse response) throws IOException {
		BackendUser user = BackendLoginUtil.getCurrentUser();
		List<Privilege> privilegeList = userRoleService.findAllByUserId(user
				.getId());

		JSONArray jsonArray = (JSONArray) JSONArray.toJSON(privilegeList);
		int size = jsonArray.size();
		for (int i = 0; i < size; i++) {
			JSONObject jsono = jsonArray.getJSONObject(i);
			MenuUtil.putName2Text(jsono);
		}
		// String str=JSONObject.toJSONString(privilegeList).replaceAll("name",
		// "text");
		String str = jsonArray.toJSONString();
		returnJsonString(response, str);
		return;
	}

}
